<template>
	<view class="goods_list">
		<view v-for="(item, index) in goodsList" :key="index" @click="gotoDetail(item)">
			<my-goods :goods="item"></my-goods>
		</view>
		<view v-show="noGoodsTipsFlag" class="noGoods_list__tips">已经到底了~</view>
	</view>
</template>

<script>
import { connnectApi } from '../../static/api'
import { showMsg } from '../../static/common'
	export default {
		data() {
			return {
				goodsList: [], 	//商品列表数据
				goodsTotal: 0,		//总数量，用来实现分页
				goodsPagenum: 0,
				options: '',
				queryObj: {
					query: '',
					cid: '',
					pagenum: 1,
					pagesize: 10,
				},
				noGoodsTipsFlag: false,
				isloading: false,
			};
		},
		onLoad(options) {
			this.options = options;
			this.getGoodsList();
		},
		onReachBottom() {
			if(this.isloading)	return;
			this.queryObj.pagenum += 1;
			this.getGoodsList();
		},
		onPullDownRefresh() {
			this.queryObj.pagenum = 1;
			this.total = 0;
			this.isloading = false;
			this.goodsList = [];
			this.getGoodsList(() => {
				uni.stopPullDownRefresh();
			});
		},
		methods: {
			async getGoodsList(onpullFunc) {
				const {query, cid } = this.options;
				this.queryObj = {
					query: query || '',
					cid: cid || '',
					pagenum: this.queryObj.pagenum || 1,
					pagesize: this.queryObj.pagesize || 10,
				};
				this.isloading = true;
				await connnectApi.getGoodsList(this.queryObj)
				.then(({ data }) => {
					if(data.meta.status !== 200) return showMsg.showErrMsg(data.meta.msg);
					onpullFunc && onpullFunc();
					const {goods, pagenum, total} = data.message;
					this.goodsList = [...this.goodsList, ...goods];
					this.noGoodsTipsFlag = goods.length === 0 ? true : false;
					this.goodsTotal = total;
					this.goodsPagenum = pagenum;
					this.isloading = false;
				})
				.catch((err) => {
					console.log(err);
				});
			},
			
			gotoDetail(goodsId) {
				uni.navigateTo({
					url: '/subpkg/goods_details/goods_details?goods_id=' + goodsId.goods_id,
				})
			},
		},
	}
</script>

<style lang="scss">
.goods_list{
	display: flex;
	flex-direction: column;
	align-items: center;
	.noGoods_list__tips {
		
		font-size: 12px;
		color: #ccc;
	}
}
</style>
